/*
 *  Copyright (C) 2011 Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * pn_Addresses.java
 *
 * Created on 29/06/2011, 23:56:15
 */
package kuasar.plugin.servermanager.gui.actions;

import java.net.InterfaceAddress;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import kuasar.plugin.Intercom.GUI;

/**
 *
 * @author Jesus Navalon i Pastor <jnavalon at redhermes dot net>
 */
public class pn_Addresses extends kuasar.plugin.classMod.AbstractPanel {

    private pn_Wizard parent;
    private DefaultListModel model = new DefaultListModel();
    private List<InterfaceAddress> list;
    private List<InterfaceAddress> selected= new ArrayList<InterfaceAddress>();
    /** Creates new form pn_Addresses */
    public pn_Addresses(pn_Wizard parent, List<InterfaceAddress> list) {
        this.list=list;
        this.parent=parent;
        initComponents();
        for(InterfaceAddress ia : list){
            model.addElement(ia.getAddress().getHostAddress() + "/" + ia.getNetworkPrefixLength());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_title = new javax.swing.JLabel();
        lbl_addresses = new javax.swing.JLabel();
        spn_addresses = new javax.swing.JScrollPane();
        lst_addresses = new javax.swing.JList();
        bt_accept = new javax.swing.JButton();

        setOpaque(false);

        lbl_title.setFont(new java.awt.Font("Dialog", 1, 18));
        lbl_title.setForeground(new java.awt.Color(204, 204, 204));
        lbl_title.setText("Select networks you wish to search");

        lbl_addresses.setForeground(new java.awt.Color(204, 204, 204));
        lbl_addresses.setText("Available networks:");

        lst_addresses.setModel(model);
        spn_addresses.setViewportView(lst_addresses);

        bt_accept.setBackground(new java.awt.Color(0, 0, 0));
        bt_accept.setForeground(new java.awt.Color(204, 204, 204));
        bt_accept.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kuasar/plugin/servermanager/icons/dialog-ok-apply.png"))); // NOI18N
        bt_accept.setText("Accept");
        bt_accept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_acceptActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addComponent(spn_addresses, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbl_title)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(lbl_addresses))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(379, Short.MAX_VALUE)
                        .addComponent(bt_accept)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbl_title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbl_addresses)
                .addGap(18, 18, 18)
                .addComponent(spn_addresses, javax.swing.GroupLayout.DEFAULT_SIZE, 209, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bt_accept)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void bt_acceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_acceptActionPerformed
        for(int i : lst_addresses.getSelectedIndices()){
            selected.add(list.get(i));
        }
        GUI.loadPlugin(parent);
        GUI.invisibleToolBar();
    }//GEN-LAST:event_bt_acceptActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_accept;
    private javax.swing.JLabel lbl_addresses;
    private javax.swing.JLabel lbl_title;
    private javax.swing.JList lst_addresses;
    private javax.swing.JScrollPane spn_addresses;
    // End of variables declaration//GEN-END:variables
    protected List<InterfaceAddress> getList(){
         return selected;
    }
}
